package com.yunji.quartz.controller;

import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.quartz.domain.SysJobLog;
import com.yunji.quartz.service.ISysJobLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度日志操作处理
 *
 * @author yunji
 */
@Api("调度日志操作处理")
@RestController
@RequestMapping("/monitor/jobLog")
public class SysJobLogController {
    @Autowired
    private ISysJobLogService jobLogService;

    /**
     * 查询定时任务调度日志列表
     */
    @ApiOperation("查询定时任务调度日志列表")
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public List<SysJobLog> list(SysJobLog sysJobLog) {
        return jobLogService.selectJobLogList(sysJobLog);
    }

    /**
     * 导出定时任务调度日志列表
     */
    @ApiOperation("导出定时任务调度日志列表")
    @PreAuthorize("@ss.hasPermi('monitor:job:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysJobLog sysJobLog) {
//        List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
//        ExcelUtil<SysJobLog> util = new ExcelUtil<>(SysJobLog.class);
//        util.exportExcel(response, list, "调度日志");
    }

    /**
     * 根据调度编号获取详细信息
     */
    @ApiOperation("根据调度编号获取详细信息")
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping(value = "/{jobLogId}")
    public Response<SysJobLog> getInfo(@PathVariable Long jobLogId) {
        return ResponseHelper.success(jobLogService.selectJobLogById(jobLogId));
    }


    /**
     * 删除定时任务调度日志
     */
    @ApiOperation("删除定时任务调度日志")
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @DeleteMapping("/{jobLogIds}")
    public Response<String> remove(@PathVariable Long[] jobLogIds) {
        Assert.isTrue(jobLogService.deleteJobLogByIds(jobLogIds) > 0, "删除失败");
        return ResponseHelper.success();
    }

    /**
     * 清空定时任务调度日志
     */
    @ApiOperation("清空定时任务调度日志")
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @DeleteMapping("/clean")
    public Response<String> clean() {
        jobLogService.cleanJobLog();
        return ResponseHelper.success();
    }
}
